package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.PersonDutyDao;
import cn.topevery.garden.entity.dto.PersonDutyDTO;
import cn.topevery.garden.entity.po.PersonAttendance;
import cn.topevery.garden.entity.po.PersonDuty;
import cn.topevery.garden.entity.po.PersonDuty;
import cn.topevery.garden.entity.query.PersonAttendanceQuery;
import cn.topevery.garden.entity.query.PersonDutyQuery;
import cn.topevery.garden.entity.vo.PersonAttendanceVO;
import cn.topevery.garden.entity.vo.PersonDutyVO;

import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.base.BaseEntityDao;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.db.base.BaseEntityDao;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * 请假/加班
 */
@Service
public class PersonDutyLogic extends BaseLogic<PersonDuty> {
    @Autowired
    PersonDutyDao personDutyDao;

    @Override
    public BaseEntityDao<PersonDuty, Integer> getDao() {
        return personDutyDao;
    }

    @Autowired
    PersonAttendanceLogic personAttendanceLogic;

    @Autowired
    private Mapper mapper;

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<PersonDutyVO> listPage(PersonDutyQuery query) {
        return personDutyDao.listPage(query);
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId,Integer[] ids) {
        personDutyDao.delete(userId, ids);
    }

    /**
     * 根据自增id保存或者更新
     *
     * @param personDuty 实体
     */
    public void saveOrUpdate(String userId, PersonDuty personDuty){
        personDutyDao.saveOrUpdate(userId,personDuty);

        PersonAttendanceQuery query = new PersonAttendanceQuery();
        query.setPageIndex(1);
        query.setPageSize(10);
        query.setUserId(personDuty.getUserId());
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = dtf2.format(personDuty.getBeginDate());
        query.setDate(format);
        DataPager<PersonAttendanceVO> personAttendanceVODataPager = personAttendanceLogic.listPage(query);
        PersonAttendanceVO personAttendanceVO = new PersonAttendanceVO();
        if(personAttendanceVODataPager.total==0){
            // 新增
            personAttendanceVO.setEntityId(UUID.randomUUID().toString());
            personAttendanceVO.setDate(LocalDate.parse(format,dtf2));
            personAttendanceVO.setUserName(personDuty.getUserName());
            personAttendanceVO.setUserId(personDuty.getUserId());
        }else{
            //修改
            personAttendanceVO = personAttendanceVODataPager.records.get(0);
        }
        if(personDuty.getType().equals(1)) {
            personAttendanceVO.setIsLeave(1);
        }
        if(personDuty.getType().equals(2)){
            personAttendanceVO.setIsOvertimes(1L);
        }
        personAttendanceLogic.saveOrUpdate(userId, mapper.map(personAttendanceVO, PersonAttendance.class));
    }

    /**
         * 根据自定义列值来获取对象
         */
    public PersonDuty getEntityByColumn(String key, String value) {
        return personDutyDao.getEntityByColumn(key, value);
    }
}
